package com.project.collectAPI.comm;

import org.apache.commons.io.IOUtils;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class JsonConfigUtil {

    public static String GlobalConfig=null;

    /**
     * <p>Title: 读取json文件 </p>
     * <p>Description: 读取json文件 </p>
     */
    private static String readJsonFile(String fileName) throws FileNotFoundException {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);
            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (FileNotFoundException e) {
            throw e;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * <p>Title: 读取外部json配置文件 </p>
     * <p>Description: 外部config目录>内部 resources下config目录 </p>
     */
    public static String resolveJsonFileToString(String fileName) {
        // 文件中的内容
        String content = "";
        try {
            // 读取jar运行时所在的目录
            ApplicationHome home = new ApplicationHome(JsonConfigUtil.class);
            String jarPath = home.getSource().getParentFile().toString();
            // 尝试读取外置config文件夹
            content = readJsonFile(jarPath + File.separator + "config" + File.separator + File.separator + fileName);
        } catch (IOException exception){
            System.out.println("jar同级config目录下未获取到配置文件{}"+fileName);
            try {
                ClassPathResource resource = new ClassPathResource("config" + File.separator + fileName);
                content = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
            }catch (IOException exception1){
                System.out.println("jar同级config目录下未获取到配置文件{}"+fileName);
            }
        }
        return content;

    }


    /**
     * <p>Title: 读取resources目录下的文件文件 </p>
     */
    public static String ReadResourceJsonFile(String FileName) throws IOException {
        File file = new File("src/main/resources/static/"+FileName);
        FileInputStream fis= new FileInputStream(file);
        InputStreamReader isr= new InputStreamReader(fis);
        BufferedReader br= new BufferedReader(isr);
        StringBuilder sb = new StringBuilder("");
        String data;
        while((data = br.readLine()) != null && !("".equals(data)))  {
            //System.out.println("数据:"+data);
            sb.append(data);
        }
        br.close();
        isr.close();
        fis.close();
        return sb.toString();
    }
}
